package engine.newUi
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.logic.loading.effect.AbstractTransitionEffect;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneChangeManager;
	import engine.ui.UIManage;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import flashx.textLayout.events.ModelChange;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.loader.SwfLoader;

	/**
	 * 
	 * @author yangcg
	 * 
	 */
	public class TipsWindowLogic
	{
		public static var _self:TipsWindowLogic
		public static function getInstance():TipsWindowLogic
		{
			if(_self==null)
			{
				_self = new TipsWindowLogic();
			}
			return _self;
		}
		private var _model:ModelLocator; 
		private var _ui:TipsWindowUI ; 
		private var _showArr:Array =[] ; 

		private var _mc:MovieClip;  
		private var arr:Array = [] ; 
		private var _count:uint =0 ; 
		public function TipsWindowLogic()
		{
			
		}
		public function Init():void
		{
			_showArr = new Array(); 
			_ui=TipsWindowUI.getInstance();
			_model = ModelLocator.getInstance(); 
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_ADVENTURE_BEFALLEN),getInfo);

		}
		public function BeforeShow():void
		{

			_showArr.push(_ui); 
			addListener();
		}
		public function AfterHide():void
		{
			_showArr.splice(0);
			removeListener();
			_mc=null;
		}
		private function addListener():void
		{
			_ui.close_btn.addEventListener(MouseEvent.CLICK,closeHandler) ; 
		}
		private function removeListener():void
		{
			_ui.close_btn.removeEventListener(MouseEvent.CLICK,closeHandler) ; 
		}
		private function getInfo(e:CustomMsgEvent):void
		{
			arr = [] ; 
//			arr = e.pList ; 
			
			if(_ui.created==false)
			{
				var arrlist:Array = e.pList
				_ui.label_1.text= StaticIni.getSwfLangStr("Task_Caption"+e.pList[0]);
				var str:String = StaticIni.getSwfLangStr(e.pList[1]);
				var index:int = uint(Math.random()*6)+4;
				_ui.input_text.htmlText= StaticIni.getSwfLangStrVar("UIString_100041"+index,[str]);
				
				
				UIManage.getInstance().maskEnable = true;
				UIManage.getInstance().setCustUIMask(StageInfo.uiSpace,0.5,true,false);
				if(GameData.Role)
				{
					GameData.Role.killMove();	
				}
				_ui.show();
				
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_TASK_CAN_ACCEPT);
//				arr  = [] ; 
			}else
			{
				arr .push(e.pList); 
			}
			
		}

		private function closeHandler(e:MouseEvent):void
		{
			UIManage.getInstance().cancelCustUIMask(true);
			_ui.hide();
			getAdture(_count) ; 
			
		}
		/**
		 * 
		 * @param value
		 * 
		 */
		private function getAdture(value:int):void
		{
			_count++;
			var arrList:Array =arr[value] ;
			if(arrList==null){
				arr = [] ; 
				_count = 0 ; 
				return  ;
			}else
			{
				_ui.label_1.text= StaticIni.getSwfLangStr("Task_Caption"+arrList[value]);
				var str:String = StaticIni.getSwfLangStr(arrList[value+1]);
				var index:int = uint(Math.random()*6)+4;
				_ui.input_text.htmlText= StaticIni.getSwfLangStrVar("UIString_100041"+index,[str]);
				UIManage.getInstance().maskEnable = true;
				UIManage.getInstance().setCustUIMask(StageInfo.uiSpace,0.5,true,false);
				GameData.Role.killMove();
				
				_ui.show();
				
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_TASK_CAN_ACCEPT);	
			}
			
		}

	}
}